home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8725 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  47 lines

  1. Newsgroups: comp.lang.c
  2. Path: cwi.nl!dik
  3. From: dik@cwi.nl (Dik T. Winter)
  4. Subject: Re: Problem Negating an Unsigned Char
  5. Message-ID: <Dntr8t.L41@cwi.nl>
  6. Sender: news@cwi.nl (The Daily Dross)
  7. Nntp-Posting-Host: chrysant.cwi.nl
  8. Organization: CWI, Amsterdam
  9. References: <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl> <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca>
  10. Date: Wed, 6 Mar 1996 02:36:29 GMT
  11.  
  12. In article <4hfmmgINN4t8@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13.  > In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
  14.  >  > > To make it portable, you must manually mask for the lower eight bits:
  15.  >  > > 
  16.  >  > >     if (a == (~b & 0xff))
  17.  >  >
  18.  >  >And how would this work on machines with other than eight bit chars?
  19.  > 
  20.  > Just fine, thank you. The C standard guarantees char to be at least eight
  21.  > bits wide. The original poster wants eight bit arithmetic; i.e. that the
  22.  > complement of 0x11 be equal to 0xEE. This is what he is testing for, and
  23.  > the above is how you get it.
  24.  
  25. Well, that is your interpretation of what the original poster wants.  See
  26. the subject line.  As I read it he just wanted to know why
  27.     a == ~b
  28. does not work and why
  29.     a == (unsigned char)(~b)
  30. does work.
  31. ...
  32.  > Casting to an unsigned quantity is implementation defined. Are you suggesting
  33.  > that the invocation of implementation defined behavior is more portable than a
  34.  > well-defined bit masking operation?
  35.  
  36. I know that negating unsigned chars with CHAR_BIT bits is difficult to do
  37. portably (see for instance Peter Seebach's posts).  And, yes, I know that
  38. casting is likely to fail on a 1's complement machine.  But of the 1's
  39. complement machines I ever did use, only one had C implemented and that
  40. was unusable as a language on that machine (due to the problems to mimic
  41. mod 2 arithmetic on unsigned amongst others).  As soon as somebody comes
  42. up with a 1's complement machine with a usable C implementation I may
  43. change my viewpoint.
  44. -- 
  45. dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098
  46. home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/
  47.